package io.renren.common.aspect;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;

import java.io.IOException;
import java.math.BigDecimal;

public class Float2Serialize extends JsonSerializer<Float> {
    @Override
    public void serialize(Float data, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
       if(null!= data){
           int roundingMode = BigDecimal.ROUND_HALF_UP;//表示四舍五入，可以选择其他舍值方式，例如去尾，等等.
           BigDecimal bd = new BigDecimal(String.valueOf(data));
           jsonGenerator.writeString(bd.setScale(2,roundingMode).toString());
       }else {
           jsonGenerator.writeString("0.00");
       }
    }
}
